import { createWebHistory, createRouter } from 'vue-router'
/* Layout */
import Layout from '@/layout'

/**
 * Note: 路由配置项
 *
 * hidden: true                     // 当设置 true 的时候该路由不会再侧边栏出现 如401，login等页面，或者如一些编辑页面/edit/1
 * alwaysShow: true                 // 当你一个路由下面的 children 声明的路由大于1个时，自动会变成嵌套的模式--如组件页面
 *                                  // 只有一个时，会将那个子路由当做根路由显示在侧边栏--如引导页面
 *                                  // 若你想不管路由下面的 children 声明的个数都显示你的根路由
 *                                  // 你可以设置 alwaysShow: true，这样它就会忽略之前定义的规则，一直显示根路由
 * redirect: noRedirect             // 当设置 noRedirect 的时候该路由在面包屑导航中不可被点击
 * name:'router-name'               // 设定路由的名字，一定要填写不然使用<keep-alive>时会出现各种问题
 * query: '{"id": 1, "name": "ry"}' // 访问路由的默认传递参数
 * roles: ['admin', 'common']       // 访问路由的角色权限
 * permissions: ['a:a:a', 'b:b:b']  // 访问路由的菜单权限
 * meta : {
    noCache: true                   // 如果设置为true，则不会被 <keep-alive> 缓存(默认 false)
    title: 'title'                  // 设置该路由在侧边栏和面包屑中展示的名字
    icon: 'svg-name'                // 设置该路由的图标，对应路径src/assets/icons/svg
    breadcrumb: false               // 如果设置为false，则不会在breadcrumb面包屑中显示
    activeMenu: '/system/user'      // 当路由设置了该属性，则会高亮相对应的侧边栏。
  }
 */

// 公共路由
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    name: 'Redirect',
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index.vue'),
        name: 'RedirectPath'
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login'),
    hidden: true
  },
  {
    path: '/register',
    component: () => import('@/views/register'),
    hidden: true
  },
  {
    path: "/:pathMatch(.*)*",
    component: () => import('@/views/error/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error/401'),
    hidden: true
  },
  {
    path: '',
    component: Layout,
    redirect: '/index',
    children: [
      {
        path: '/index',
        component: () => import('@/views/index'),
        name: 'Index',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    path: '/user',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [
      {
        path: 'profile',
        component: () => import('@/views/system/user/profile/index'),
        name: 'Profile',
        meta: { title: '个人中心', icon: 'user' }
      }
    ]
  },

  // 系统管理
  {
    path: '/core',
    component: Layout,
    name: 'Core',
    meta: { title: '系统管理', icon: 'dashboard' },
    children: [
      {
        path: 'user',
        component: () => import('@/views/core/user/index'),
        name: 'CoreUser',
        meta: { title: '用户管理', }
      },
      {
        path: 'dept',
        component: () => import('@/views/core/dept/index'),
        name: 'CoreDept',
        meta: { title: '机构管理', }
      },
      {
        path: 'role',
        component: () => import('@/views/core/role/index'),
        name: 'CoreRole',
        meta: { title: '权限管理', }
      },
      {
        path: 'base',
        component: () => import('@/views/core/base/index'),
        name: 'CoreBase',
        meta: { title: '基础管理', }
      },
      {
        path: 'password',
        component: () => import('@/views/core/password/index'),
        name: 'CorePassword',
        meta: { title: '密码管理', }
      },
      {
        path: 'notice',
        component: () => import('@/views/core/notice/index'),
        name: 'CoreNotice',
        meta: { title: '公告管理', }
      },
      {
        path: 'message',
        component: () => import('@/views/core/message/index'),
        name: 'CoreMessage',
        meta: { title: '消息管理' }
      },
    ]
  },
  // 在系统管理和质量计划管理之间添加查询统计模块
  {
    path: '/statistics',
    component: Layout,
    name: 'Statistics',
    meta: { title: '查询统计模块', icon: 'dashboard' },
    children: [
      {
        path: 'query',
        component: () => import('@/views/statistics/query/index'),
        name: 'StatisticsQuery',
        meta: { title: '查询统计模块' }
      }
    ]
  },
  // 质量计划管理


  // 文件模板管理
  {
    path: '/document',
    component: Layout,
    redirect: '/document/template/file/formulate',
    name: 'Document',
    meta: { title: '文件模板管理', icon: 'dashboard' },
    children: [
      {
        path: 'template',
        name: 'DocumentTemplate',  // 修改
        redirect: '/document/template/file/formulate',
        meta: { title: '文件管理' },
        children: [
          {
            path: 'file/formulate',
            component: () => import('@/views/document/template/file/formulate'),
            name: 'DocumentTemplateFileFormulate',  // 修改
            meta: { title: '制定' }
          },
          // 注释的路由也需要修改
          // {
          //   path: 'file/approve',
          //   component: () => import('@/views/document/template/file/approve'),
          //   name: 'DocumentTemplateFileApprove',  // 修改
          //   meta: { title: '审批' }
          // },
          // {
          //   path: 'file/authorize',
          //   component: () => import('@/views/document/template/file/authorize'),
          //   name: 'DocumentTemplateFileAuthorize',  // 修改
          //   meta: { title: '批准' }
          // },
          {
            path: 'file/execute',
            component: () => import('@/views/document/template/file/execute'),
            name: 'DocumentTemplateFileExecute',  // 修改
            meta: { title: '执行' }
          }
        ]
      }
    ]
  },

  // 流程配置管理
  {
    path: '/process',
    component: Layout,
    redirect: '/process/management/formulate',
    name: 'ProcessConfig',
    meta: { title: '流程配置管理', icon: 'dashboard' },
    children: [
      {
        path: 'management',
        redirect: '/process/management/formulate',
        name: 'ProcessConfigManagement',  // 修改
        meta: { title: '流程管理', icon: 'Operation' },
        children: [
          {
            path: 'formulate',
            component: () => import('@/views/process/management/formulate'),
            name: 'ProcessConfigManagementFormulate',  // 修改
            meta: { title: '制定' }
          },
          {
            path: 'config',
            component: () => import('@/views/process/management/config'),
            name: 'ProcessConfigManagementConfig',  // 修改
            meta: { title: '流程配置' }
          }
        ]
      },
    ]
  },

  // 人员管理
  {
    path: '/staff',
    component: Layout,
    name: 'Staff',
    meta: { title: '人员管理', icon: 'dashboard' },
    children: [
      {
        path: 'profile',
        name: 'StaffProfile',
        redirect: '/staff/profile/entry',
        meta: { title: '人员资质档案', icon: 'Files' },
        children: [
          {
            path: 'entry',
            component: () => import('@/views/staff/profile/entry'),
            name: 'StaffProfileEntry',  // 修改
            meta: { title: '人员录入' }
          },
          // {
          //   path: 'approve',
          //   component: () => import('@/views/staff/profile/approve'),
          //   name: 'StaffProfileApprove',  // 修改
          //   meta: { title: '人员审批' }
          // }
        ]
      },
      {
        path: 'change',
        name: 'StaffChange',
        redirect: '/staff/change/draft',
        meta: { title: '人员变动文件管理', icon: 'Document' },
        children: [
          {
            path: 'draft',
            component: () => import('@/views/staff/change/draft'),
            name: 'StaffChangeDraft',  // 修改
            meta: { title: '变动文件拟定' }
          },
          {
            path: 'approve',
            component: () => import('@/views/staff/change/approve'),
            name: 'StaffChangeApprove',  // 修改
            meta: { title: '人员变动审批' }
          },
          {
            path: 'result',
            component: () => import('@/views/staff/change/result'),
            name: 'StaffChangeResult',  // 修改
            meta: { title: '执行结果' }
          }
        ]
      }
    ]
  },

  // 程序管理文件
  {
    path: '/program',
    component: Layout,
    name: 'Program',
    meta: { title: '程序管理文件', icon: 'dashboard' },
    children: [
      {
        path: 'quality',
        redirect: '/quality/plan/formulate',
        name: 'ProgramQuality',
        meta: { title: '质量计划管理', },
        children: [
          {
            path: 'plan/formulate',
            component: () => import('@/views/program/plan/formulate'),
            name: 'QualityPlanFormulate',
            meta: { title: '制定' }
          },
          {
            path: 'plan/approve',
            component: () => import('@/views/program/plan/approve'),
            name: 'QualityPlanApprove',
            meta: { title: '审批' }
          },
          {
            path: 'plan/authorize',
            component: () => import('@/views/program/plan/authorize'),
            name: 'QualityPlanAuthorize',
            meta: { title: '批准' }
          },
          {
            path: 'plan/execute',
            component: () => import('@/views/program/plan/execute'),
            name: 'QualityPlanExecute',
            meta: { title: '执行' }
          }
        ]
      },
      {
        path: 'software',
        name: 'ProgramSoftware',  // 修改
        redirect: '/program/software/formulate',
        meta: { title: '自动化软件和数据管理', },
        children: [
          {
            path: 'formulate',
            component: () => import('@/views/program/software/formulate'),
            name: 'ProgramSoftwareFormulate',  // 修改
            meta: { title: '制定' }
          },
          {
            path: 'approve',
            component: () => import('@/views/program/software/approve'),
            name: 'ProgramSoftwareApprove',  // 修改
            meta: { title: '审批' }
          },
          {
            path: 'authorize',
            component: () => import('@/views/program/software/authorize'),
            name: 'ProgramSoftwareAuthorize',  // 修改
            meta: { title: '批准' }
          },
          {
            path: 'execute',
            component: () => import('@/views/program/software/execute'),
            name: 'ProgramSoftwareExecute',  // 修改
            meta: { title: '执行' }
          }
        ]
      },
      {
        path: 'comprehensive',
        redirect: '/program/comprehensive/control/formulate',
        name: 'ProgramComprehensive',  // 修改
        meta: { title: '综合文件管理', },
        children: [
          {
            path: 'control',
            name: 'ProgramComprehensiveControl',  // 修改
            redirect: '/program/comprehensive/control/formulate',
            meta: { title: '文件控制程序', icon: 'Document' },
            children: [
              {
                path: 'formulate',
                component: () => import('@/views/program/comprehensive/control/formulate'),
                name: 'ProgramComprehensiveControlFormulate',  // 修改
                meta: { title: '制定' }
              },
              {
                path: 'approve',
                component: () => import('@/views/program/comprehensive/control/approve'),
                name: 'ProgramComprehensiveControlApprove',  // 修改
                meta: { title: '审批' }
              },
              {
                path: 'authorize',
                component: () => import('@/views/program/comprehensive/control/authorize'),
                name: 'ProgramComprehensiveControlAuthorize',  // 修改
                meta: { title: '批准' }
              },
              {
                path: 'execute',
                component: () => import('@/views/program/comprehensive/control/execute'),
                name: 'ProgramComprehensiveControlExecute',  // 修改
                meta: { title: '执行' }
              }
            ]
          },
          {
            path: 'archive',
            name: 'ProgramComprehensiveArchive',  // 修改
            meta: { title: '文档管理', },
            children: [
              {
                path: 'formulate',
                component: () => import('@/views/program/comprehensive/archive/formulate'),
                name: 'ProgramComprehensiveArchiveFormulate',  // 修改
                meta: { title: '制定' }
              },
              {
                path: 'publish',
                component: () => import('@/views/program/comprehensive/archive/publish'),
                name: 'ProgramComprehensiveArchivePublish',  // 修改
                meta: { title: '发布' }
              },
              {
                path: 'execute',
                component: () => import('@/views/program/comprehensive/archive/execute'),
                name: 'ProgramComprehensiveArchiveExecute',  // 修改
                meta: { title: '执行' }
              }
            ]
          },
          {
            path: 'record',
            name: 'ProgramComprehensiveRecord',  // 修改
            meta: { title: '记录控制程序', },
            children: [
              {
                path: 'formulate',
                component: () => import('@/views/program/comprehensive/record/formulate'),
                name: 'ProgramComprehensiveRecordFormulate',  // 修改
                meta: { title: '制定' }
              },
              {
                path: 'approve',
                component: () => import('@/views/program/comprehensive/record/approve'),
                name: 'ProgramComprehensiveRecordApprove',  // 修改
                meta: { title: '审批' }
              },
              {
                path: 'authorize',
                component: () => import('@/views/program/comprehensive/record/authorize'),
                name: 'ProgramComprehensiveRecordAuthorize',  // 修改
                meta: { title: '批准' }
              },
              {
                path: 'execute',
                component: () => import('@/views/program/comprehensive/record/execute'),
                name: 'ProgramComprehensiveRecordExecute',  // 修改
                meta: { title: '执行' }
              }
            ]
          }
        ]
      },
      // 其他程序管理文件子路由
      {
        path: 'protection',
        name: 'ProgramProtection',  // 修改
        redirect: '/program/protection/formulate',
        meta: { title: '保护顾客机密和所有权管理程序', },
        children: [
          {
            path: 'formulate',
            component: () => import('@/views/program/protection/formulate'),
            name: 'ProgramProtectionFormulate',  // 修改
            meta: { title: '制定' }
          },
          {
            path: 'approve',
            component: () => import('@/views/program/protection/approve'),
            name: 'ProgramProtectionApprove',  // 修改
            meta: { title: '审批' }
          },
          {
            path: 'authorize',
            component: () => import('@/views/program/protection/authorize'),
            name: 'ProgramProtectionAuthorize',  // 修改
            meta: { title: '批准' }
          },
          {
            path: 'execute',
            component: () => import('@/views/program/protection/execute'),
            name: 'ProgramProtectionExecute',  // 修改
            meta: { title: '执行' }
          }
        ]
      },
      {
        path: 'safety',
        name: 'Safety',
        redirect: '/program/safety/formulate',
        meta: { title: '安全管理程序', },
        children: [
          {
            path: 'formulate',
            component: () => import('@/views/program/safety/formulate'),
            name: 'SafetyFormulate',
            meta: { title: '制定' }
          },
          {
            path: 'approve',
            component: () => import('@/views/program/safety/approve'),
            name: 'SafetyApprove',
            meta: { title: '审批' }
          },
          {
            path: 'authorize',
            component: () => import('@/views/program/safety/authorize'),
            name: 'SafetyAuthorize',
            meta: { title: '批准' }
          },
          {
            path: 'execute',
            component: () => import('@/views/program/safety/execute'),
            name: 'SafetyExecute',
            meta: { title: '执行' }
          }
        ]
      },
      {
        path: 'prevention',
        name: 'Prevention',
        redirect: '/program/prevention/formulate',
        meta: { title: '纠正预防管理', },
        children: [
          {
            path: 'formulate',
            component: () => import('@/views/program/prevention/formulate'),
            name: 'PreventionFormulate',
            meta: { title: '制定' }
          },
          {
            path: 'approve',
            component: () => import('@/views/program/prevention/approve'),
            name: 'PreventionApprove',
            meta: { title: '审批' }
          },
          {
            path: 'authorize',
            component: () => import('@/views/program/prevention/authorize'),
            name: 'PreventionAuthorize',
            meta: { title: '批准' }
          },
          {
            path: 'execute',
            component: () => import('@/views/program/prevention/execute'),
            name: 'PreventionExecute',
            meta: { title: '执行' }
          }
        ]
      },
      // 顾客满意度管理
      {
        path: 'satisfaction',
        name: 'Satisfaction',
        redirect: '/program/satisfaction/formulate',
        meta: { title: '顾客满意度管理', },
        children: [
          {
            path: 'formulate',
            component: () => import('@/views/program/satisfaction/formulate'),
            name: 'SatisfactionFormulate',
            meta: { title: '制定' }
          },
          {
            path: 'approve',
            component: () => import('@/views/program/satisfaction/approve'),
            name: 'SatisfactionApprove',
            meta: { title: '审批' }
          },
          {
            path: 'authorize',
            component: () => import('@/views/program/satisfaction/authorize'),
            name: 'SatisfactionAuthorize',
            meta: { title: '批准' }
          },
          {
            path: 'execute',
            component: () => import('@/views/program/satisfaction/execute'),
            name: 'SatisfactionExecute',
            meta: { title: '执行' }
          }
        ]
      },
      // 合同采购管理
      {
        path: 'contract',
        name: 'Contract',
        redirect: '/program/contract/formulate',
        meta: { title: '合同采购管理', },
        children: [
          {
            path: 'formulate',
            component: () => import('@/views/program/contract/formulate'),
            name: 'ContractFormulate',
            meta: { title: '制定' }
          },
          {
            path: 'approve',
            component: () => import('@/views/program/contract/approve'),
            name: 'ContractApprove',
            meta: { title: '审批' }
          },
          {
            path: 'authorize',
            component: () => import('@/views/program/contract/authorize'),
            name: 'ContractAuthorize',
            meta: { title: '批准' }
          },
          {
            path: 'execute',
            component: () => import('@/views/program/contract/execute'),
            name: 'ContractExecute',
            meta: { title: '执行' }
          }
        ]
      },
      // 不确定度管理
      {
        path: 'uncertainty',
        name: 'Uncertainty',
        redirect: '/program/uncertainty/formulate',
        meta: { title: '不确定度管理', },
        children: [
          {
            path: 'formulate',
            component: () => import('@/views/program/uncertainty/formulate'),
            name: 'UncertaintyFormulate',
            meta: { title: '制定' }
          },
          {
            path: 'approve',
            component: () => import('@/views/program/uncertainty/approve'),
            name: 'UncertaintyApprove',
            meta: { title: '审批' }
          },
          {
            path: 'authorize',
            component: () => import('@/views/program/uncertainty/authorize'),
            name: 'UncertaintyAuthorize',
            meta: { title: '批准' }
          },
          {
            path: 'execute',
            component: () => import('@/views/program/uncertainty/execute'),
            name: 'UncertaintyExecute',
            meta: { title: '执行' }
          }
        ]
      },
      // 方法的验证和确认
      {
        path: 'validation',
        name: 'Validation',
        redirect: '/program/validation/formulate',
        meta: { title: '方法的验证和确认', },
        children: [
          {
            path: 'formulate',
            component: () => import('@/views/program/validation/formulate'),
            name: 'ValidationFormulate',
            meta: { title: '制定' }
          },
          {
            path: 'approve',
            component: () => import('@/views/program/validation/approve'),
            name: 'ValidationApprove',
            meta: { title: '审批' }
          },
          {
            path: 'authorize',
            component: () => import('@/views/program/validation/authorize'),
            name: 'ValidationAuthorize',
            meta: { title: '批准' }
          },
          {
            path: 'execute',
            component: () => import('@/views/program/validation/execute'),
            name: 'ValidationExecute',
            meta: { title: '执行' }
          }
        ]
      },
      // 计量建标管理
      {
        path: 'measurement',
        name: 'Measurement',
        redirect: '/program/measurement/formulate',
        meta: { title: '计量建标管理', },
        children: [
          {
            path: 'formulate',
            component: () => import('@/views/program/measurement/formulate'),
            name: 'MeasurementFormulate',
            meta: { title: '制定' }
          },
          {
            path: 'approve',
            component: () => import('@/views/program/measurement/approve'),
            name: 'MeasurementApprove',
            meta: { title: '审批' }
          },
          {
            path: 'authorize',
            component: () => import('@/views/program/measurement/authorize'),
            name: 'MeasurementAuthorize',
            meta: { title: '批准' }
          },
          {
            path: 'execute',
            component: () => import('@/views/program/measurement/execute'),
            name: 'MeasurementExecute',
            meta: { title: '执行' }
          }
        ]
      },
      // 机构授权管理
      {
        path: 'authorization',
        name: 'Authorization',
        redirect: '/program/authorization/formulate',
        meta: { title: '机构授权管理', },
        children: [
          {
            path: 'formulate',
            component: () => import('@/views/program/authorization/formulate'),
            name: 'AuthorizationFormulate',
            meta: { title: '制定' }
          },
          {
            path: 'approve',
            component: () => import('@/views/program/authorization/approve'),
            name: 'AuthorizationApprove',
            meta: { title: '审批' }
          },
          {
            path: 'authorize',
            component: () => import('@/views/program/authorization/authorize'),
            name: 'AuthorizationAuthorize',
            meta: { title: '批准' }
          },
          {
            path: 'execute',
            component: () => import('@/views/program/authorization/execute'),
            name: 'AuthorizationExecute',
            meta: { title: '执行' }
          }
        ]
      },
    ]
  },

  {
    path: '/equipment',
    component: Layout,
    redirect: '/equipment/manage',
    name: 'Equipment',
    meta: { title: '设备台账模块', icon: 'dashboard' },
    children: [
      {
        path: 'manage',
        component: () => import('@/views/equipment/index'),
        name: 'EquipmentManage',
        meta: { title: '设备台账模块' }
      }
    ]
  }

]

// 动态路由，基于用户权限动态去加载
export const dynamicRoutes = [
  {
    path: '/system/user-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:user:edit'],
    children: [
      {
        path: 'role/:userId(\\d+)',
        component: () => import('@/views/system/user/authRole'),
        name: 'AuthRole',
        meta: { title: '分配角色', activeMenu: '/system/user' }
      }
    ]
  },
  {
    path: '/system/role-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:role:edit'],
    children: [
      {
        path: 'user/:roleId(\\d+)',
        component: () => import('@/views/system/role/authUser'),
        name: 'AuthUser',
        meta: { title: '分配用户', activeMenu: '/system/role' }
      }
    ]
  },
  {
    path: '/monitor/job-log',
    component: Layout,
    hidden: true,
    permissions: ['monitor:job:list'],
    children: [
      {
        path: 'index/:jobId(\\d+)',
        component: () => import('@/views/monitor/job/log'),
        name: 'JobLog',
        meta: { title: '调度日志', activeMenu: '/monitor/job' }
      }
    ]
  },
  {
    path: '/tool/gen-edit',
    component: Layout,
    hidden: true,
    permissions: ['tool:gen:edit'],
    children: [
      {
        path: 'index/:tableId(\\d+)',
        component: () => import('@/views/tool/gen/editTable'),
        name: 'GenEdit',
        meta: { title: '修改生成配置', activeMenu: '/tool/gen' }
      }
    ]
  },
]

const router = createRouter({
  history: createWebHistory(),
  routes: constantRoutes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  },
});

export default router;
